package haizhi.fruitmall.service.impl;

import haizhi.fruitmall.dao.mapper.AuthMapper;
import haizhi.fruitmall.dao.mapper.RoleAuthMapper;
import haizhi.fruitmall.dao.mapper.RoleMapper;
import haizhi.fruitmall.pojo.po.Auth;
import haizhi.fruitmall.pojo.po.Role;
import haizhi.fruitmall.pojo.po.RoleAuth;
import haizhi.fruitmall.pojo.po.RoleAuthExample;
import haizhi.fruitmall.pojo.po.RoleExample;
import haizhi.fruitmall.service.RoleService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RoleServiceImpl implements RoleService {
	@Autowired
	private RoleMapper roleMapper;

	@Autowired
	private AuthMapper authMapper;

	@Autowired
	private RoleAuthMapper roleAuthMapper;

	@Override
	public void addRole(Role role) {
		roleMapper.insert(role);
	}

	@Override
	public void addRoleAuth(List<RoleAuth> roleAuths) {
		for (RoleAuth roleAuth : roleAuths) {
			roleAuthMapper.insert(roleAuth);
		}
	}

	@Override
	public void deleteRoleAuth(Integer role_id, Integer[] auth_ids) {
		for (Integer auth_id : auth_ids) {
			RoleAuthExample roleAuthExample = new RoleAuthExample();
			RoleAuthExample.Criteria criteria = roleAuthExample
					.createCriteria();
			criteria.andRoleIdEqualTo(role_id);
			criteria.andAuthIdEqualTo(auth_id);
			roleAuthMapper.deleteByExample(roleAuthExample);
		}
	}

	@Override
	public void updateRoleName(Role role, String role_name) {
		RoleExample roleExample = new RoleExample();
		RoleExample.Criteria criteria = roleExample.createCriteria();
		criteria.andRoleNameEqualTo(role_name);
		roleMapper.updateByExampleSelective(role, roleExample);
	}

	@Override
	public List<Role> getRoles(String role_name) {
		RoleExample roleExample = new RoleExample();
		RoleExample.Criteria criteria = roleExample.createCriteria();
		criteria.andRoleNameEqualTo(role_name);
		return roleMapper.selectByExample(roleExample);
	}

	@Override
	public Role getRoleByRoleId(Integer role_id) {
		return roleMapper.selectByPrimaryKey(role_id);
	}

	@Override
	public Map<String, List<String>> getRoleAuthNames(String role_name) {

		Map<String, List<String>> authMap = new HashMap<String, List<String>>();
		// 根据角色名获得角色id
		Integer role_id = getRoles(role_name).get(0).getRoleId();

		// 根据角色id获得角色权限关系
		List<RoleAuth> role_auths = getRoleAuths(role_id);

		for (RoleAuth role_auth : role_auths) {
			Auth auth = authMapper.selectByPrimaryKey(role_auth.getAuthId());
			if (auth.getAuthPid() == null
					&& authMap.get(auth.getAuthC()) == null) {
				List<String> authAs = new ArrayList<String>();
				authMap.put(auth.getAuthC(), authAs);
			} else {
				if (authMap.get(auth.getAuthC()) == null) {
					List<String> authAs = new ArrayList<String>();
					authAs.add(auth.getAuthA());
					authMap.put(auth.getAuthC(), authAs);
				} else {
					authMap.get(auth.getAuthC()).add(auth.getAuthA());
				}
			}
		}
		return authMap;
	}

	@Override
	public List<RoleAuth> getRoleAuths(Integer role_id) {
		RoleAuthExample roleAuthExample = new RoleAuthExample();
		RoleAuthExample.Criteria criteria = roleAuthExample.createCriteria();
		criteria.andRoleIdEqualTo(role_id);
		return roleAuthMapper.selectByExample(roleAuthExample);
	}

}
